package com.machinecontroller.service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;

import com.easivend.evprotocol.EVprotocol;
import com.machinecontroller.R;
import com.machinecontroller.constant.McConfig;
import com.machinecontroller.constant.UrlConstant;
import com.machinecontroller.model.EVModel;
import com.machinecontroller.model.ResultModel;
import com.machinecontroller.utils.CommonUtils;
import com.machinecontroller.utils.DevUtil;
import com.machinecontroller.utils.EVControlerUtil;
import com.machinecontroller.utils.GsonUtil;
import com.machinecontroller.utils.Logger;
import com.machinecontroller.utils.ServiceUitl;
import com.sunshine.retrofit.HttpBuilder;
import com.tencent.bugly.beta.Beta;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

import static com.machinecontroller.utils.CommonUtils.parseObject;

/**
 * 后台心跳服务
 */
public class HeartService extends Service {


    private static final String TAG = "HeartService";

    public HeartService() {
    }

    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
 /*
         * 此线程用监听Service2的状态
		 */
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                Map<String, String> map = new HashMap<>();
                String imei = DevUtil.getImei(getApplicationContext());
//        https://github.com/hust-MC/SlienceInstall.git
                map.put("version", DevUtil.getVersion(getApplicationContext()));
                map.put("devid", TextUtils.isEmpty(imei) ? "" : imei);
                map.put("openstatus", EVControlerUtil.getDoorState());
                new HttpBuilder(UrlConstant.API_GET_DOOR_STATUS)
                        .params(map)
                        .params("key", "value")
                        .tag(this)//需要取消请求的tag
                        .success(str -> {//请求成功返回的数据
                            Log.e(TAG, "心跳"+str);
                            ResultModel model = GsonUtil.praseModel(str, ResultModel.class);
                            if (model != null) {
                                if (model.getCode().equals("0")) {
                                    Logger.e(TAG, "心跳一次");
                                    return;
                                }
                            }
                            Logger.e(TAG, "心跳异常");
                        })
                        .error(e -> {//请求失败返回的数据
                            Log.e(TAG, "心跳异常 ==> " + e.toString());

                        })
                        .get();
            }
        }, McConfig.MC_HEART_TIME, McConfig.MC_HEART_TIME);
    }



    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
